package modulos.usuario.vista;

import java.awt.Dimension;
import java.awt.Toolkit;
import javax.swing.JOptionPane;
import modelo.Sistema;

/**
 *
 * @author caesar
 */
public class iFrmConfig extends javax.swing.JDialog {

    Sistema eSistema = new Sistema();

    public iFrmConfig(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
        Dimension scrn = Toolkit.getDefaultToolkit().getScreenSize();
        Dimension windw = this.getSize();
        this.setLocation((scrn.width - windw.width) / 2, (scrn.height - windw.height) / 2);
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        btnCerrar = new javax.swing.JButton();
        pnlPermisos = new javax.swing.JPanel();
        btnCambiar = new javax.swing.JButton();
        cmbAcceso = new javax.swing.JComboBox();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Configuraciones de acceso:");
        setModal(true);
        setResizable(false);
        getContentPane().setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        btnCerrar.setFont(new java.awt.Font("Ubuntu", 0, 14)); // NOI18N
        btnCerrar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/jpgs/agt_home.png"))); // NOI18N
        btnCerrar.setText("Cerrar");
        btnCerrar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCerrarActionPerformed(evt);
            }
        });
        getContentPane().add(btnCerrar, new org.netbeans.lib.awtextra.AbsoluteConstraints(210, 140, 111, -1));

        pnlPermisos.setBorder(javax.swing.BorderFactory.createTitledBorder(javax.swing.BorderFactory.createEtchedBorder(), "Ingresar al sistema a través de:", javax.swing.border.TitledBorder.LEFT, javax.swing.border.TitledBorder.TOP, null, null));

        btnCambiar.setFont(new java.awt.Font("Ubuntu", 0, 14)); // NOI18N
        btnCambiar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/jpgs/refresh.png"))); // NOI18N
        btnCambiar.setText("Cambiar");
        btnCambiar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCambiarActionPerformed(evt);
            }
        });

        cmbAcceso.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Acceso abierto", "Acceso con clave" }));
        cmbAcceso.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cmbAccesoActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout pnlPermisosLayout = new javax.swing.GroupLayout(pnlPermisos);
        pnlPermisos.setLayout(pnlPermisosLayout);
        pnlPermisosLayout.setHorizontalGroup(
            pnlPermisosLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnlPermisosLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(cmbAcceso, 0, 158, Short.MAX_VALUE)
                .addGap(18, 18, 18)
                .addComponent(btnCambiar, javax.swing.GroupLayout.PREFERRED_SIZE, 112, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );
        pnlPermisosLayout.setVerticalGroup(
            pnlPermisosLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnlPermisosLayout.createSequentialGroup()
                .addGap(22, 22, 22)
                .addGroup(pnlPermisosLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnCambiar)
                    .addComponent(cmbAcceso, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(36, Short.MAX_VALUE))
        );

        getContentPane().add(pnlPermisos, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 20, -1, 112));

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private Sistema establecerParametrosSistema() {
        Sistema s = new Sistema();
        s.setLoguear(cmbAcceso.getSelectedIndex());
        return s;
    }

    private void btnCerrarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCerrarActionPerformed
        this.dispose();
    }//GEN-LAST:event_btnCerrarActionPerformed

    private void btnCambiarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCambiarActionPerformed
        try {
            if (JOptionPane.showConfirmDialog(null, "¿Está seguro de cambiar la forma de acceso al sistema?", "Configuración", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE) == 0) {
                if (eSistema.updateParametrosSys(this.establecerParametrosSistema())) {
                    JOptionPane.showMessageDialog(null, "Cambios de configuración actualizados correctamente ...", "Configuración", JOptionPane.INFORMATION_MESSAGE);
                    btnCerrar.requestFocus();
                } else {
                    JOptionPane.showMessageDialog(null, "Los cambios en la configuración no se han podido realizar ...", "Configuración", JOptionPane.WARNING_MESSAGE);
                    btnCambiar.requestFocus();
                }
            }
        } catch (Exception ex) {
            JOptionPane.showMessageDialog(null, "Se ha producido la excepcion: \n" + ex.getMessage(), "Configuración", JOptionPane.WARNING_MESSAGE);
        }
    }//GEN-LAST:event_btnCambiarActionPerformed

    private void cmbAccesoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cmbAccesoActionPerformed
        btnCambiar.requestFocus();
    }//GEN-LAST:event_cmbAccesoActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnCambiar;
    private javax.swing.JButton btnCerrar;
    private javax.swing.JComboBox cmbAcceso;
    private javax.swing.JPanel pnlPermisos;
    // End of variables declaration//GEN-END:variables
}
